package com.vinci.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import org.apache.tomcat.util.http.mapper.Mapper;

import com.vinci.beans.*;

public class EcModel extends ModelDAO{

	Connection connection =null;
	ResultSet resultSet = null; 
	private ArrayList<Ec> allElementC = new ArrayList<Ec>();
	
	public int AjouterEc(Ec elementConst){

		PreparedStatement prepStat = null;
		String laRequete = null;
		int valErreur = 0;

		try{

			connection = super.getConnection();
			laRequete = "INSERT INTO ec_se (APOGEE,LIBELLE_EC,H_CM_EC,H_TD_EC,H_TP_EC,TYPE_EC) VALUES(?,?,?,?,?,?)";
			prepStat = connection.prepareStatement(laRequete);


			prepStat.setString(1, elementConst.getAPOGEE());
			prepStat.setString(2, elementConst.getLIBELLE_EC());
			prepStat.setInt(3, elementConst.getH_CM_EC());
			prepStat.setInt(4, elementConst.getH_TD_EC());
			prepStat.setInt(5, elementConst.getH_TP_EC());
			prepStat.setString(6, elementConst.getTYPE_EC());
			

			valErreur = prepStat.executeUpdate();

		}catch(Exception ex)
		{
			valErreur = 0;
			
			ex.printStackTrace();
		}

		try{
			prepStat.close();
			connection.close();
		}catch(Exception ex){
			System.out.println("Impossible de fermer un objet");
			ex.printStackTrace();
		}

		return valErreur;
	}

	public Ec getEc(String APOGEE){
		PreparedStatement prepStat = null;
		Ec e_c = null;
		String laRequete = null;

		try{
			connection = super.getConnection();
			laRequete = "SELECT * FROM ec_se WHERE APOGEE=? ";
			prepStat =connection.prepareStatement(laRequete);
			prepStat.setString(1, APOGEE);
			
			resultSet = prepStat.executeQuery();

			if (resultSet!=null){
				if (resultSet.next()){
					e_c = mapperEc(resultSet);
				}
			}else
				System.out.println("Ec introuvable dans la base");

		}catch(Exception ex){
			e_c =null;
			System.out.println("erreur dans la requette" + ex);
		}

		try{
			resultSet.close();
			prepStat.close();
			connection.close();
		}catch(Exception ex){
			System.out.println("Impossible de fermer un objet");
			ex.printStackTrace();
		}

		return e_c;
	}

	

	
	public ArrayList<Ec> getAllEc(){
		PreparedStatement prepStat = null;
		Ec e_c = null;
		String laRequete = null;

		try{
			connection = super.getConnection();
			laRequete = "SELECT * FROM ec_se";
			prepStat =connection.prepareStatement(laRequete);
			
			resultSet = prepStat.executeQuery();
			System.out.println("Attttttttttttttt");
			if (resultSet!=null){
				while (resultSet.next()){
					e_c = mapperEc(resultSet);
					allElementC.add(e_c);
					
					System.out.println("Erreur lors del'insertion d'un objet "+ ""+e_c.getAPOGEE() 
						+" "	+e_c.getH_CM_EC()
						+" "	+e_c.getH_TD_EC()
						+" "	+e_c.getH_TP_EC()
						+" "	+e_c.getLIBELLE_EC()
							
							);
				
				
				}
			}else
				System.out.println("Aucun eelement constitutif (ec) dans la base");

		}catch(Exception ex){
			e_c =null;
			System.out.println("erreur dans la requette " + ex);
		}

		try{
			resultSet.close();
			prepStat.close();
			connection.close();
		}catch(Exception ex){
			System.out.println("Impossible de fermer un objet");
			ex.printStackTrace();
		}

		return allElementC;
	}	
	
	public Ec mapperEc(ResultSet resultSet){
		Ec e_c = new Ec();

		try
		{
			if(resultSet.getString("APOGEE")==null){
				e_c.setAPOGEE("");
			}
			else
			{
				e_c.setAPOGEE(resultSet.getString("APOGEE"));
			}

			if(resultSet.getString("LIBELLE_EC")==null){
				e_c.setLIBELLE_EC("");
			}
			else
			{
				e_c.setLIBELLE_EC(resultSet.getString("LIBELLE_EC"));
			}

			if(resultSet.getInt("H_CM_EC")== 0){
				e_c.setH_CM_EC(0);
			}
			else
			{
				e_c.setH_CM_EC(resultSet.getInt("H_CM_EC"));
			}
			
			if(resultSet.getInt("H_TD_EC")== 0){
				e_c.setH_TD_EC(0);
			}
			else
			{
				e_c.setH_TD_EC(resultSet.getInt("H_TD_EC"));
			}

			if(resultSet.getInt("H_TP_EC")==0){
				e_c.setH_TP_EC(0);
			}
			else
			{
				e_c.setH_TP_EC(resultSet.getInt("H_TP_EC"));
			}

			if(resultSet.getString("TYPE_EC")== null){
				e_c.setTYPE_EC("");
			}
			else
			{
				e_c.setTYPE_EC(resultSet.getString("TYPE_EC"));
			}


		}catch(Exception ex)
		{
			e_c = null;
			System.out.println("Erreur lors du mapping des attributs"+ex);
		}
		return e_c;
	}
	
}
